package managedbean.SCM.MaterialOrder;

import java.util.Arrays;
import javax.ejb.EJB;
import entity.SCM.MaterialLine;
import entity.SCM.MaterialRequestTemp;
import entity.SCM.Supplier;
import java.util.ArrayList;
import java.util.List;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.faces.event.PhaseEvent;
import session.stateless.SCM.MaterialOrderBeanLocal;
import session.stateless.SCM.MaterialRequestBeanLocal;
import session.stateless.SCM.SupplierBeanLocal;

@ManagedBean(name = "addMaterialOrder")
@ViewScoped
public class AddMaterialOrderFromRequestManagedBean {
    
    @EJB
    private SupplierBeanLocal supplierBeanLocal;
    @EJB
    private MaterialOrderBeanLocal materialOrderBeanLocal;
    @EJB
    private MaterialRequestBeanLocal materialRequestBeanLocal;
    private List<MaterialRequestTemp> materialRequestToProcess;
    private MaterialLine[] selectedMaterialLines;
    private List<MaterialLine> materialLineList = new ArrayList<MaterialLine>(); 
    private String selectedSupplier;
    private List<String> suppliers = new ArrayList<String>();
    private String statusMessage;
    private MaterialRequestTemp selectedMaterialRequest;
    private List<Supplier> temp;
    
    public AddMaterialOrderFromRequestManagedBean() {
    }

    public void initView(PhaseEvent event){
        if(materialRequestToProcess==null){
            try{
            materialRequestToProcess = materialRequestBeanLocal.getMaterialRequestNeedProcess();
            if(materialRequestToProcess==null){
                statusMessage="No Material Request Need To Be Processed";    
                FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO,statusMessage, ""));    
            }
            }catch(Exception ex){
                System.out.print("wwwwwwwwwwwwww");
            }
        }
    } 

    public List<MaterialLine> getMaterialLineList() {
        if(selectedMaterialLines!=null){
            materialLineList = Arrays.asList(selectedMaterialLines);
        }
        return materialLineList;
    }

    public MaterialRequestTemp getSelectedMaterialRequest() {
        return selectedMaterialRequest;
    }

    public void setSelectedMaterialRequest(MaterialRequestTemp selectedMaterialRequest) {
        this.selectedMaterialRequest = selectedMaterialRequest;
    }   

    public List<MaterialRequestTemp> getMaterialRequestToProcess() {
        return materialRequestToProcess;
    }

    public MaterialLine[] getSelectedMaterialLines() {
        return selectedMaterialLines;
    }

    public void setSelectedMaterialLines(MaterialLine[] selectedMaterialLines) {
        this.selectedMaterialLines = selectedMaterialLines;
    }

    public String getSelectedSupplier() {
        return selectedSupplier;
    }

    public void setSelectedSupplier(String selectedSupplier) {
        this.selectedSupplier = selectedSupplier;
    }

    public List<String> getSuppliers() {
        if(materialLineList!=null){
            temp = supplierBeanLocal.getSupplierForOrder(this.getMaterialLineList());          
            if(temp==null){
                statusMessage = "No such supplier provide all the materials";
                FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR,statusMessage, ""));
                return null;
            }else{
                suppliers = new ArrayList<String>();
                for(int i=0; i<temp.size();i++){
                    suppliers.add(temp.get(i).getSupplierName());
                }  
            }
        }
        return suppliers;   
    }

    public void createMaterialOrder(ActionEvent event){
        try{ 
            if(materialLineList!=null & selectedSupplier!=null){
                Long id = materialOrderBeanLocal.createMaterialOrder(selectedSupplier, 
                        materialLineList, selectedMaterialRequest.getMaterialRequestTempId() );
                if(id==null){
                    statusMessage = "Add Unsuccessfully";
                    FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, statusMessage , ""));
                }else{
                    statusMessage = "Add Successfully";
                    materialRequestBeanLocal.removeMaterialLines(selectedMaterialRequest, materialLineList);
                    FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO,"Material Order: " + id + statusMessage, ""));
                }
            }else if(materialLineList==null){
                statusMessage = "Choose Material Line First";
                FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO,statusMessage, ""));
            }else if(this.suppliers==null){
                statusMessage = "Choose Supplier First";
                FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO,statusMessage, ""));
            }
        }catch(Exception ex){
            System.out.printf("$$$$$$$$$$$$");
        }
    }
}
